package com.www.applyword.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.bean.GoodListBean;
import com.www.applyword.bean.LoginBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ImageUtil;
import com.www.applyword.utils.SPUtil;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GirdDropDownAdpter4 extends BaseAdapter {
    private Context context;
    private List<GoodListBean.IntegerGoodsListBean> beanList;
    private LayoutInflater inflater;
    private GoodListBean.IntegerGoodsListBean integerGoodsListBean;


    public GirdDropDownAdpter4(Context context, List<GoodListBean.IntegerGoodsListBean> beanList) {
        this.context = context;
        this.beanList = beanList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_job_work3,null);
            holder.tv_fen = convertView.findViewById(R.id.tv_fen);
            holder.item_city_name_tv = convertView.findViewById(R.id.tv);
            holder.iv = convertView.findViewById(R.id.iv);
            holder.tv_fen = convertView.findViewById(R.id.tv_fen);
            holder.sb_reply = convertView.findViewById(R.id.sb_reply);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        integerGoodsListBean = beanList.get(position);
        holder.item_city_name_tv.setText(integerGoodsListBean.getIntegerGoods_Name());
        ImageUtil.setImage(holder.iv,integerGoodsListBean.getIntegerGoods_Img());
        holder.tv_fen.setText(integerGoodsListBean.getIntegerGoods_Grade() + "级");

        holder.sb_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (integerGoodsListBean.getIntegerGoods_Sort() == 0){
                     Utils.showToastSafe("不可兑换");
                     return;
                }

                Map<String, Object> params = new HashMap<>();
                params.put("UserID", SPUtil.getString(Constants.USERID));
                params.put("GoodsID",integerGoodsListBean.getIntegerGoods_ID());

                HttpClient.get(context, Constants.EXCHANGEGOODS, params,new CallBack<LoginBean>() {
                    @Override
                    public void onSuccess(LoginBean result) {
                        Utils.showToastSafe("兑换成功");
                    }
                });
            }
        });


        //0可兑换
       /* if (integerGoodsListBean.getIntegerGoods_Sort() == 0){
            holder.sb_reply.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("UserID", SPUtil.getString(Constants.USERID));
                    params.put("GoodsID",integerGoodsListBean.getIntegerGoods_ID());

                    HttpClient.get(context, Constants.EXCHANGEGOODS, params,new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            Utils.showToastSafe("兑换成功");
                        }
                    });
                }
            });
        }else {

        }*/


        return convertView;
    }

    class ViewHolder {
        private TextView item_city_name_tv,tv_fen;
        private ImageView iv;
        private SuperButton sb_reply;
    }
}
